<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/29 0029
 * Time: 9:59
 */
namespace App\FreeBird\Controllers\Admin;
use App\Base\Controllers\Controller;
use App\FreeBird\Contracts\StoreUserInterface;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/**
 *
 * Class StoreUserController
 * @package App\FreeBird\Controllers\Admin
 */
class StoreUserController extends Controller
{

    /**
     * 用户操作
     * @return StoreUserInterface
     */
    public function getStoreUserInterface(){
        return app()->make(StoreUserInterface::class);
    }


    public function index(Request $request){
        if($request->getMethod()=='POST'){
            $where=function($query) use ($request){
                $username=$request->input("username");
                if(!empty($username)){
                    $query->where("username","like",'%'.$username.'%');
                }
                $apply_status=$request->input("apply_status","");
                if($apply_status!=''){
                    $query->where("apply_status",$apply_status);
                }
            };
            return  $this->getStoreUserInterface()->getListHasUser($where);
        }
        return view("freebird.store_user.index");
    }

    /**
     * 审核通过
     * @param Request $request
     */
    public function applyTrue(Request $request){
        if($request->getMethod()=='POST') {
            $validator = Validator::make($request->all(), [
                'id' => 'required',
            ], ["id.required" => 'ID必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $id = $request->input("id");
            $where = function ($query) use ($id) {
                $query->where("id", $id);
            };
            $data["apply_status"] = '1';
            //审请通过后，就更新用户权限级
            return $this->getStoreUserInterface()->updateUserInfoApply($where, $data);
        }
    }

    public function applyError(Request $request){
        if($request->getMethod()=='POST') {
            $validator = Validator::make($request->all(), [
                'id' => 'required',
                'msg' => 'required',
            ], ["id.required" => 'ID必须有值', "msg.required" => '拒绝理由必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $id = $request->input("id");
            $where = function ($query) use ($id) {
                $query->where("id", $id);
            };
            $msg=$request->input("msg");
            $data["apply_status"] = '-1';
            $data["apply_err_msg"] = $msg;
            return $this->getStoreUserInterface()->updateUserInfo($where, $data);
        }
    }

    /**
     * 详细
     * @param Request $request
     */
    public function show(Request $request){
        $where=function($query) use ($request){
            $id=$request->input("id");
            $query->where("id",$id);
        };
        $info= $this->getStoreUserInterface()->getUserInfoAndUser($where);

        return view("freebird.store_user.show")->with("info",$info);
    }

}